package com.yboot.ssocas.login;

import lombok.extern.slf4j.Slf4j;
import org.jasig.cas.client.util.CommonUtils;
import org.springframework.util.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Slf4j
public class LocalUserInfoFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request_ = (HttpServletRequest)request;
        HttpServletResponse response_ = (HttpServletResponse)response;
        String loginName = CASUtil.getAccountNameFromCas(request_);
        if(!StringUtils.isEmpty(loginName)){
            log.info("访问者 ：" +loginName);
//            request_.getSession().setAttribute("loginName", loginName);
            HttpSession session = request_.getSession();
            String jsessionid = session.getId();
            System.out.println("doFilter.jsessionid = " + jsessionid);
            Person newPerson = new Person();
            newPerson.setUuid(jsessionid);
            newPerson.setPname(loginName);
            ThreadLocalUtil.setResources(newPerson);
        }
//        String urlToRedirectTo = CommonUtils.constructRedirectUrl(this.casServerLoginUrl, this.getProtocol().getServiceParameterName(), modifiedServiceUrl, this.renew, this.gateway)

        chain.doFilter(request, response);
    }
    @Override
    public void destroy() {

    }
}
